2020-2021 Sunseeker Telemetry and Lighting System
eusci_a_spi_ex1_master.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //*****************************************************************************
74 //*****************************************************************************
75 
76 #include "driverlib.h"
77 
78 uint8_t RXData = 0;
79 uint8_t TXData = 0;
80 
81 void main(void)
82 {
83  volatile uint16_t i;
84 
85  //Stop watchdog timer
86  WDT_A_hold(WDT_A_BASE);
87 
88  /*
89  * Select Port 1
90  * Set Pin 0 as output
91  */
92  GPIO_setAsOutputPin(
93  GPIO_PORT_P1,
94  GPIO_PIN0
95  );
96  /*
97  * Select Port 1
98  * Set Pin 0 to output Low.
99  */
101  GPIO_PORT_P1,
102  GPIO_PIN0
103  );
104 
105  //Set DCO frequency to max DCO setting
106  CS_setDCOFreq(CS_DCORSEL_0, CS_DCOFSEL_3);
107  //Select DCO as the clock source for SMCLK with no frequency divider
108  CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1);
109 
110  /*
111  * Select Port 1
112  * Set Pin 5 to input Secondary Module Function, (UCA0CLK).
113  */
114  GPIO_setAsPeripheralModuleFunctionInputPin(
115  GPIO_PORT_P1,
116  GPIO_PIN5,
117  GPIO_SECONDARY_MODULE_FUNCTION
118  );
119 
120  /*
121  * Select Port 2
122  * Set Pin 0, 1 to input Secondary Module Function, (UCA0TXD/UCA0SIMO, UCA0RXD/UCA0SOMI).
123  */
124  GPIO_setAsPeripheralModuleFunctionInputPin(
125  GPIO_PORT_P2,
126  GPIO_PIN0 + GPIO_PIN1,
127  GPIO_SECONDARY_MODULE_FUNCTION
128  );
129 
130  /*
131  * Disable the GPIO power-on default high-impedance mode to activate
132  * previously configured port settings
133  */
134  PMM_unlockLPM5();
135 
136  //Initialize Master
137  EUSCI_A_SPI_initMasterParam param = {0};
138  param.selectClockSource = EUSCI_A_SPI_CLOCKSOURCE_SMCLK;
139  param.clockSourceFrequency = CS_getSMCLK();
140  param.desiredSpiClock = 500000;
141  param.msbFirst = EUSCI_A_SPI_MSB_FIRST;
142  param.clockPhase = EUSCI_A_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT;
143  param.clockPolarity = EUSCI_A_SPI_CLOCKPOLARITY_INACTIVITY_HIGH;
144  param.spiMode = EUSCI_A_SPI_3PIN;
145  EUSCI_A_SPI_initMaster(EUSCI_A0_BASE, &param);
146 
147  //Enable SPI module
148  EUSCI_A_SPI_enable(EUSCI_A0_BASE);
149 
150  EUSCI_A_SPI_clearInterrupt(EUSCI_A0_BASE,
151  EUSCI_A_SPI_RECEIVE_INTERRUPT);
152  // Enable USCI_A0 RX interrupt
153  EUSCI_A_SPI_enableInterrupt(EUSCI_A0_BASE,
154  EUSCI_A_SPI_RECEIVE_INTERRUPT);
155 
156  //Wait for slave to initialize
157  __delay_cycles(100);
158 
159  TXData = 0x1; // Holds TX data
160 
161  //USCI_A0 TX buffer ready?
162  while (!EUSCI_A_SPI_getInterruptStatus(EUSCI_A0_BASE,
163  EUSCI_A_SPI_TRANSMIT_INTERRUPT)) ;
164 
165  //Transmit Data to slave
166  EUSCI_A_SPI_transmitData(EUSCI_A0_BASE, TXData);
167 
168  __bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupts
169  __no_operation(); // Remain in LPM0
170 }
171 
172 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
173 #pragma vector=USCI_A0_VECTOR
174 __interrupt
175 #elif defined(__GNUC__)
176 __attribute__((interrupt(USCI_A0_VECTOR)))
177 #endif
178 void USCI_A0_ISR(void)
179 {
180  switch(__even_in_range(UCA0IV, USCI_SPI_UCTXIFG))
181  {
182  case USCI_SPI_UCRXIFG: // UCRXIFG
183  //USCI_A0 TX buffer ready?
184  while (!EUSCI_A_SPI_getInterruptStatus(EUSCI_A0_BASE,
185  EUSCI_A_SPI_TRANSMIT_INTERRUPT));
186 
187  RXData = EUSCI_A_SPI_receiveData(EUSCI_A0_BASE);
188 
189  //Increment data
190  TXData++;
191 
192  //Send next value
193  EUSCI_A_SPI_transmitData(EUSCI_A0_BASE,
194  TXData
195  );
196 
197  //Delay between transmissions for slave to process information
198  __delay_cycles(40);
199  break;
200  default:
201  break;
202  }
203 }
204 
uint8_t RXData
void main(void)
void USCI_A0_ISR(void)
uint8_t TXData
MPU_initThreeSegmentsParam param
__no_operation()
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)